//Author: Jiong Ye
//Email:  dexxaye@gmail.com
//Date:   8/18/2009

$(document).ready(function(){
	$('.ShadowMenu:first').each(function(){
		var thisMenu = $(this);
		var parentWrapper = $(this).parent();
		var menuHeight = $(this).height();
		var current = null;
		var floater = null;
		var menuOffset = $(this).offset();
		
		//add floater element
		$(this).prepend('<div id="f_'+(Math.floor(Math.random()*1001))+'" class="Floater">&nbsp;</div>');
		floater = $(this).children('div.Floater');
		floater.css('cssFloat','none');
		floater.hide();
		
		//move floater on mouse over
		$(this).children('.ShadowMenuChild').each(function(){
			$(this).hoverIntent(
				function(){
					moveFloaterToCurrent(thisMenu,$(this),floater);
					if($(this).children('ul').length)
					{
						$(this).children('ul').slideDown();
					}
				},function(e){
					$(this).children('ul').slideUp();
					e.stopPropagation();
					return false;
				});
		});
		
		//handle sub menu item hovering
		$('.ShadowSubmenu').children('li').each(function(){
			$(this).hoverIntent(
				function()
				{
					moveFloaterToCurrent(thisMenu,$(this),floater);
				},
				function()
				{
					
				}
			);
		});
		
		//recover floater position
		$(this).hoverIntent(
			function(){},
			function(e){
				moveFloaterToCurrent(thisMenu,current,floater);
			}
		);
		
		//show current element
		if($(this).children('.current').length)
			current = $(this).children('.current:first');
		
	});
	setTimeout('initializeMenu()',1000);
	
});
function initializeMenu()
{
	moveFloaterToCurrent($('.ShadowMenu'),$('.ShadowMenu').children('.current'),$('.ShadowMenu').children('.Floater'));
}
function moveFloaterToCurrent(menu,current,floater)
{
	var menuOffset = menu.offset();
	var menuHeight = current.height();
	var currentOffset = current.offset();
	var currentWidth = current.width();
	var currentLeft = currentOffset.left - menuOffset.left;
	var currentTop = currentOffset.top - menuOffset.top;
	var floatPadTop = parseInt(current.css("padding-top"), 10);
	var floatPadLeft = parseInt(current.css("padding-left"), 10);
	var floatPadRight = parseInt(current.css("padding-right"), 10);
	var floatPadBottom = parseInt(current.css("padding-bottom"), 10);

	floater.show();
	floater.css('padding-top',floatPadTop);
	floater.css('padding-left',floatPadLeft);
	floater.css('padding-right',floatPadRight);
	floater.css('padding-bottom',floatPadBottom);
	floater.animate({
		width: currentWidth,
		height: menuHeight,
		left: currentLeft,
		top: currentTop
	},
	200,
	'swing',
	function(){
		html = current.html();
		floater.html(html.replace(/(<ul.*>[\s\S]*<\/ul>)/i,""));
	});
}